layui.use(['form','tree','laydate','element','table','laytpl','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    //场景数据
    var treeData = {
        elem: '#tree' //指定元素
        , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
        , click: function (item) { //点击节点回调
            //查询当前场景下的设备
            $.get("/equipment/query?sceneId=" + item.id,function (data) {
                //刷新右侧设备列表和检测项目列表
                $.get("/equipment/query?sceneId=" + item.id,function (data) {
                    $("#equipment-list").html('');
                    $("#equipment-list").append('<option value="">请选择设备</option>');
                    $.each(data.data,function (i,ele) {
                        $("#equipment-list").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                    });
                    form.render('select');
                });
            });
        },
        nodes:null
    };
    //异步加载设备所处场景数据
    $.get('/scene/tree',function(data) {
        if (data.data.length != 0) {
            data.data[0].spread = true;
            var children = data.data[0].children;
            if (children != null && children.length != 0) {
                $.each(children,function (i, ele) {
                    ele.spread = true;
                });
            }
        }
        treeData.nodes = data.data;
        layui.tree(treeData);
        $("#tree-scenery").hide();
    });

    //查询条件
    //采集设备数据
    $.get("/equipment/list",function (data) {
        $.each(data.data,function (i,ele) {
            $("#equipment-list").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
        });
        form.render('select');

        //设备的选择
        form.on('select(equipment-list)',function (data) {
            $("#tips").hide();
            //查询当前设备的通道并且查询相关的预警阈值
            //列表数据
            var option = {
                elem: '#tableList',
                url : '/equipment/channelAWarn/query?equipmentId=' + data.value,
                page : false,
                // height : 500,
                limits : [10,15,20,25],
                limit : 15,
                id : "listTable",
                cols : [[
                    {field: 'equipmentName', title: '通道名称', minWidth:100, align:"center", templet:function(d){
                            var itemName = "";
                            $.ajax({
                                url : '/checkitem/' + d.checkItemId,
                                async:false,
                                success: function (data) {
                                    itemName = data.name;
                                }
                            });
                            return itemName == null?'':itemName;
                        }
                    },
                    {field: 'maxValue', title: '最大值', minWidth:100,edit:true, align:"center"},
                    {field: 'minValue', title: '最小值', minWidth:100,edit:true, align:"center"},
                    {field: 'isUse', title: '是否启用', minWidth:100, align:"center",templet: function(d){
                            return d.isUse == 1 ? '<span id="status-'+ d.id +'" style="color:#13a5da;">已启用</span>':'<span id="status-'+ d.id +'"  style="color:#ccc;">已关闭</span>';
                        }
                    },
                    {title: '操作', minWidth:175,align:"center",templet: function(d){
                            var openBtn='<a class="layui-btn layui-btn-xs layui-btn-normal" id="open-'+ d.id +'" lay-event="open">启用</a>';
                            var closeBtn = '<a class="layui-btn layui-btn-xs layui-btn-danger" id="close-'+ d.id +'" lay-event="close">关闭</a>';
                            if (d.isUse == 1) {
                                return closeBtn;
                            } else {
                                return openBtn;
                            }
                        }
                    }
                ]]
            };
            window.tableIns = table.render(option);

            table.on('edit(tableList)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                $.get("/warning/config/edit?id=" + obj.data.id + "&maxValue=" + obj.data.maxValue + "&minValue=" + obj.data.minValue,function(data){
                    if (data.code == 200) {
                        layer.msg('修改成功！');
                        tableIns.reload();
                    }
                });
            });
        });

    });

    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'open'){
            var openBtn = $("#open-" + data.id);
            openBtn.html('<i style="font-size: 16px !important;" class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i>');
            $.get("/warning/config/edit?id=" + data.id + "&isUse=1",function(data){
                if (data.code == 200) {
                    layer.msg('开启成功！');
                    tableIns.reload();
                }
            });
        } else if(layEvent === 'close'){
            var closeBtn =  $("#close-" + data.id);
            closeBtn.html('<i style="font-size: 16px !important;" class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i>');
            $.get("/warning/config/edit?id=" + data.id + "&isUse=0",function(data){
                if (data.code == 200) {
                    layer.msg('关闭成功！');
                    tableIns.reload();
                }
            });
        }
    });

});